home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14433 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  153 lines

  1. Path: rover.ucs.ualberta.ca!news
  2. From: ryangall@gpu.srv.ualberta.ca (Ryan Gallagher)
  3. Newsgroups: comp.lang.c++,de.comp.lang.c++,ualberta.cmput.201
  4. Subject: Another function pointer from a class question?!
  5. Date: 31 Mar 1996 11:59:21 GMT
  6. Organization: University of Alberta, Edmonton, Canada
  7. Message-ID: <4jls2p$bac@pulp.ucs.ualberta.ca>
  8. NNTP-Posting-Host: async6-4.remote.ualberta.ca
  9. Mime-Version: 1.0
  10. X-Newsreader: WinVN 0.99.2
  11.  
  12.  
  13. I have an error that I am unsure of.....I want to have a derived class
  14. that I can pass a function pointer that I will use in a print function.
  15. If I dont use the print function, and just have the class print off the
  16. elements in the main function, then it works fine....the class itself 
  17. works
  18. well....its just that I get a linker error when I try and run it when 
  19. using
  20. the print function. Since I want to be able to use any structure for
  21. the list, I will have to supply some sort of printing function to the 
  22. class,
  23. otherwise, it will not nessesarily know what to do when printing each 
  24. member.
  25. Anyways, this is my first attempt at a template, and derived class....so
  26. bear with me if I've made any really stupid mistakes.
  27.  
  28.  
  29. in list.h....I have the pure virtual class
  30.  
  31. template<class T>
  32. class list{
  33.  
  34.         public:
  35.  
  36.         virtual T    Add(T)=0;
  37.         virtual T    Remove(T)=0;
  38.         virtual T    POPfirst()=0;
  39.         virtual T    POPlast()=0;
  40.         virtual int  Inlist(T)=0;
  41.         virtual int  isempty()=0;
  42.         virtual void print(void (*)(T))=0;
  43.  
  44. };
  45.  
  46.  
  47. ...................................
  48.  
  49. //this file is dl_list.cpp
  50.  
  51. #include "list.h"
  52.  
  53. template<class T>
  54. // I used to have this next line, as the delaration but it didnt work,
  55. //class dl_list : public list<T>{
  56. // it kept saying that I couldnt use a abstract class directly.
  57.  
  58. class dl_list{
  59.    struct node{
  60.          T data;
  61.          node *next;
  62.          node *prev;
  63.          node(T d,node *n,node *p) { data = d; next = n; prev =p;}
  64.    };
  65.  
  66.    node *S;
  67.    node *F;
  68.    node *C;
  69.  
  70. public:
  71.  
  72.    dl_list();
  73.   ~dl_list();
  74.  
  75.    int  Add(T);
  76.    T Remove();
  77.    T POPfirst();
  78.    T POPlast();
  79.    int  empty() { return (F==S); }
  80.    void print(void (*)(T));
  81.  
  82. };
  83.  
  84. /***********************************************************************
  85. *
  86. *  print: prints off the data in the list.
  87. *
  88. ************************************************************************/
  89.  
  90. template<class T> void list<T>::print(void (*pfunc)(T))
  91.  {
  92.   node *X=S;
  93.  
  94.    cout << endl << endl <<endl;
  95.    cout << "THE LIST" << endl << "--------" << endl;
  96.  
  97.  
  98.      if(X!=F)
  99.  
  100.      do
  101.      {
  102.         X=X->next;
  103.         (*pfunc)(X->data);
  104.      }
  105.      while( X != F && X);
  106.  
  107.   return 0;
  108.  
  109.  }
  110.  
  111. //*************************************************************
  112.  
  113. void printint(int d)
  114. {
  115.  printf("( %i )\n",d);
  116. }
  117.  
  118. //*************************************************************
  119.  
  120. int main(void)
  121. {
  122.  int R[10]={1,2,3,4,5,6,7,8,9,0};
  123.  int n;
  124.  
  125.  dl_list<int>   M;
  126.  clrscr();
  127.  
  128.    for(n=0; n<10; n++)
  129.    {
  130.      M.Add(R[n]);
  131.    }
  132.  
  133.    M.print(printint);
  134.  
  135.  
  136.  
  137. }
  138. *****************LINKER ERROR IS ****************
  139.  
  140. undefined symbol dl_list<int>::print( void (near*)(int)) in module 
  141. DL_LIST.CPP
  142.  
  143.  
  144.  
  145.  
  146. I hope you can help me out...........
  147. thanks alot, Mail me back if possible!
  148. ------------------------------------------
  149.  
  150.  
  151. Ryan G
  152.  
  153.